
local skel = fk.CreateSkill {
  name = "rmt__chenwei",
  tags = { Skill.Switch },
}

Fk:loadTranslationTable{
  ["rmt__chenwei"] = "谶纬",
  [":rmt__chenwei"] = "转换技，阳：当你使用一张背置牌时，你可以令一名角色将你的一张手牌翻面；"..
    "阴：当你使用一张非背置牌时，你可以将获得一名其他角色的牌并将此牌背置。",

  [":rmt__chenwei_yang"] = "转换技，<font color=\"#E0DB2F\">阳：当你使用一张背置牌时，你可以令一名角色将你的一张手牌翻面；</font>"..
    "阴：当你使用一张非背置牌时，你可以将获得一名其他角色的牌并将此牌背置。",

  [":rmt__chenwei_yin"] = "转换技，阳：当你使用一张背置牌时，你可以令一名角色将你的一张手牌翻面；"..
    "<font color=\"#E0DB2F\">阴：当你使用一张非背置牌时，你可以将获得一名其他角色的牌并将此牌背置。</font>",

  ["#rmt__chenwei-yang"] = "谶纬：你可以另一名角色将你的一张手牌翻面",
  ["#rmt__chenwei-yin"] = "谶纬：你可以将获得一名其他角色的牌并将此牌背置",

  ["$rmt__chenwei1"] = "",
  ["$rmt__chenwei2"] = "",
}

skel:addEffect(fk.CardUsing, {
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      if player:getSwitchSkillState(skel.name, false) == fk.SwitchYang then
        return table.contains(data.card.skillNames, "rmt__yaoyiy") and not player:isKongcheng()
      else
        return not table.contains(data.card.skillNames, "rmt__yaoyiy") and table.find(player.room.alive_players, function(p)
          return p ~= player and not p:isNude()
        end)
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = room.alive_players
    local choice = "yang"
    if player:getSwitchSkillState(skel.name, false) == fk.SwitchYin then
      targets = table.filter(targets, function(p)
        return p ~= player and not p:isNude()
      end)
      choice = "yin"
    end
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = targets,
      skill_name = skel.name,
      prompt = "#rmt__chenwei-" .. choice,
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, { tos = to, choice = choice })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    if event:getCostData(self).choice == "yang" then
      local cid = room:askToChooseCard(to, {
        target = player,
        flag = "h",
        skill_name = skel.name,
      })
      local card = Fk:getCardById(cid)
      room:setCardMark(card, "rmt__yaoyiy-inhand", 1-card:getMark("rmt__yaoyiy-inhand"))
    else
      local cid = room:askToChooseCard(player, {
        target = to,
        flag = "he",
        skill_name = skel.name,
      })
      room:obtainCard(player, cid, false, fk.ReasonPrey, player, skel.name, "rmt__yaoyiy-inhand")
    end
  end,
})

return skel
